//****************************************************************
//* Copyright (c) 2008 Ford Motor Company. All Rights Reserved.
//*
//* $$Workfile:   CurrencyDao.java  $$
//* $$Revision:   1.1  $$
//* $$Author:   JLOPE262  $$
//* $$Date:   Nov 21 2008 14:24:44  $$
//*
//*****************************************************************
package ar.com.ford.it.fullEconomics.persistence.dao;

import java.util.Collection;
import ar.com.ford.it.fullEconomics.persistence.dto.Teesm01Currency;
import ar.com.ford.it.webengine.persistence.PersistenceUtil;
import com.ford.it.logging.ILogger;
import com.ford.it.logging.Level;
import com.ford.it.logging.LogFactory;
import com.ford.it.persistence.PersistenceException;
import com.ford.it.persistence.PersistenceManager;
import com.ford.it.persistence.Query;
import com.ford.it.persistence.Transaction;

public class CurrencyDao extends Dao {

    private static final String CLASS_NAME = CurrencyDao.class.getName();
    private static final ILogger log = LogFactory.getInstance().getLogger(
            CLASS_NAME);

    public Teesm01Currency findCurrencyByCode(String ecoCurrenCode)
            throws PersistenceException {

        String METHOD_NAME = "findCurrencyByCode";
        log.entering(CLASS_NAME, METHOD_NAME);

        Teesm01Currency result = new Teesm01Currency();
        Teesm01Currency pk = new Teesm01Currency();
        pk.setEesm01CurrencyCodeC(ecoCurrenCode);

        result = (Teesm01Currency) super.getObjById(pk);

        log.exiting(CLASS_NAME, METHOD_NAME);
        return result;
    }

    public Collection<Teesm01Currency> findAllCurrency()
            throws PersistenceException {

        String METHOD_NAME = "findAllCurrency";
        log.entering(CLASS_NAME, METHOD_NAME);

        Collection<Teesm01Currency> resCol = null;

        resCol = super.findAll(new Teesm01Currency(), null, null);

        log.exiting(CLASS_NAME, METHOD_NAME);
        return resCol;
    }

    /**
     * Da de alta el Currency pasado por parametro
     * 
     * @param currency
     * Debe tener cargado el nombre del Currency
     */
    public void createCurrency(Teesm01Currency currency)
            throws PersistenceException {

        String METHOD_NAME = "createCurrency";
        log.entering(CLASS_NAME, METHOD_NAME);

        Transaction tx = null;

        if (this.findCurrencyByCode(currency.getEesm01CurrencyCodeC()) == null) {

            tx = PersistenceUtil.getTransaction();

            tx.registerInsert(currency);

        } else {
            log.logp(Level.WARNING, CLASS_NAME, METHOD_NAME,
                    "the currency code already exists", currency
                            .getEesm01CurrencyCodeC());
        }

        log.exiting(CLASS_NAME, METHOD_NAME);
    }

    /**
     * Devuelve el currency cuyo code es currencyCode listo para realizarle las
     * actualizaciones pertinentes
     * 
     * @param currencyCode
     * Code del Currency a actualizar
     * @return
     * @throws PersistenceException
     */
    public Teesm01Currency getCurrencyForUpdate(String currencyCode)
            throws PersistenceException {

        String METHOD_NAME = "getCurrencyForUpdate";
        log.entering(CLASS_NAME, METHOD_NAME);

        Teesm01Currency result = null;
        Teesm01Currency pk = new Teesm01Currency();

        pk.setEesm01CurrencyCodeC(currencyCode);
        result = (Teesm01Currency) super.getObjForUpdate(pk);

        log.exiting(CLASS_NAME, METHOD_NAME);
        return result;
    }

    /**
     * Elimina de forma definitiva de la base de datos el currency pasado por
     * parametro
     * 
     * @param currency
     * Debe haber sido obtenido de la base de datos
     */
    public void deleteCurrency(Teesm01Currency currency)
            throws PersistenceException {

        String METHOD_NAME = "deleteCurrency";
        log.entering(CLASS_NAME, METHOD_NAME);

        Transaction tx = PersistenceUtil.getTransaction();
        if (tx != null) {
            tx.registerDelete(currency);
        }

        log.exiting(CLASS_NAME, METHOD_NAME);
    }

    /**
     * Busca Currencys segun code
     * 
     * @param code
     * @return
     * @throws PersistenceException
     */
    public Collection<Teesm01Currency> findCurrencys(Teesm01Currency currency)
            throws PersistenceException {

        String METHOD_NAME = "findCurrencys";
        log.entering(CLASS_NAME, METHOD_NAME);

        Collection<Teesm01Currency> resultCol = null;
        PersistenceManager pm = null;

        pm = PersistenceUtil.getPersistenceManager();
        Query query = pm.newQuery("findCurrencys", Teesm01Currency.class);
        query.setParameters(currency);

        resultCol = (Collection) pm.executeQuery(query);

        log.exiting(CLASS_NAME, METHOD_NAME);
        return resultCol;
    }
}
